<template>
  <div class="card-container">
    <!-- 搜索区域 -->
    <div class="search-container">
      <span class="search-label">车牌号码：</span>
      <el-input v-model="dataParams.carNumber" clearable placeholder="请输入内容" class="search-main" />
      <span class="search-label">车主姓名：</span>
      <el-input v-model="dataParams.personName" clearable placeholder="请输入内容" class="search-main" />
      <span class="search-label">状态：</span>
      <el-select v-model="dataParams.cardStatus">
        <el-option
          v-for="item in selectStatus"
          :key="item.id"
          :label="item.value"
          :value="item.id"
        />
      </el-select>
      <el-button type="primary" class="search-btn" @click="serchList">查询</el-button>
    </div>
    <!-- 新增删除操作区域 -->
    <div class="create-container">
      <el-button type="primary" @click="$router.push('/cardadd')">添加月卡</el-button>
      <el-button @click="delCards">批量删除</el-button>
    </div>
    <!-- 表格区域 -->
    <div class="table">
      <el-table style="width: 100%" :data="tableData" @selection-change="handleSelectionChange">
        <el-table-column
          type="selection"
          width="55"
        />
        <el-table-column type="index" label="序号" />
        <el-table-column label="车主名称" prop="personName" />
        <el-table-column label="联系方式" prop="phoneNumber" />
        <el-table-column label="车牌号码" prop="carNumber" />
        <el-table-column label="车辆品牌" prop="carBrand" />
        <el-table-column label="剩余有效天数" prop="totalEffectiveDate" />
        <el-table-column label="状态" prop="cardStatus" :formatter="formatStatus" />
        <el-table-column label="操作" fixed="right" width="180">
          <template #default="{ row }">
            <el-button size="mini" type="text" @click="renewCard(row)">续费</el-button>
            <el-button size="mini" type="text" @click="lookCard(row)">查看</el-button>
            <el-button size="mini" type="text" @click="editCard(row)">编辑</el-button>
            <el-button size="mini" type="text" @click="delCard(row)">删除</el-button>
          </template>
        </el-table-column>
      </el-table>
    </div>
    <div class="page-container">
      <!-- current-change：切换页码的事件 -->
      <el-pagination
        layout="total, prev,sizes, pager, next"
        :page-size="dataParams.pageSize"
        :total="total"
        :page-sizes="[2, 5,10,20]"
        @current-change="pageChage"
        @size-change="handleSizeChange"
      />
    </div>
  </div>
</template>

<script>
import { delCardAPI, getCardListAPI } from '@/api/card'
export default {
  data() {
    return {
      // 定义成对象，可以直接通过接口发给后台
      dataParams: {
        page: 1,
        pageSize: 2,
        carNumber: '',
        personName: '',
        cardStatus: ''
      },
      tableData: [],
      total: 0,
      selectStatus: [
        {
          id: 0,
          value: '可用'
        },
        {
          id: 1,
          value: '已过期'
        }
      ],
      multipleSelection: []
    }
  },
  created() {
    this.getCardList()
  },
  methods: {
    async getCardList() {
      const res = await getCardListAPI(this.dataParams)
      console.log(res)
      this.tableData = res.data.rows
      this.total = res.data.total
    },
    // 分页器页码切换
    pageChage(val) {
      this.dataParams.page = val
      this.getCardList()
    },
    formatStatus(row, column, cellValue, index) {
      console.log(row, column, cellValue, index)
      const statusMap = {
        0: '可用',
        1: '已过期'
      }
      return statusMap[cellValue]
    },
    handleSizeChange(val) {
      console.log(`每页 ${val} 条`)
      this.dataParams.pageSize = val
      this.getCardList()
    },
    serchList() {
      this.dataParams.page = 1
      this.getCardList()
    },
    editCard(row) {
      console.log(row.id)
      this.$router.push({
        path: '/cardadd',
        query: {
          id: row.id
        }
      })
    },
    handleSelectionChange(val) {
      this.multipleSelection = val
    },
    // 单个删除
    delCard(row) {
      this.$confirm('确认删除吗?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(async() => {
        const res = await delCardAPI([row.id])
        if (res.code === 10000) {
          this.$message.success('删除成功')
          // 更新列表数据
          this.serchList()
        }
      }).catch(err => {
        console.log(err)
      })
    },
    // 批量删除
    delCards() {
      const ids = this.multipleSelection.map(item => item.id)
      console.log(ids)
      this.$confirm('确认删除吗?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(async() => {
        const res = await delCardAPI(ids)
        if (res.code !== 10000) {
          this.$message.success('删除成功' + res.msg)
          this.serchList()
        }
      }).catch(err => {
        console.log(err)
      })
    },
    // 续费
    renewCard(row) {
      console.log(row.id)
      this.$router.push({
        path: '/renewCard',
        query: {
          id: row.id
        }
      })
    },
    // 查看详情
    lookCard(row) {
      console.log(row.id)
      this.$router.push({
        path: '/lookCard',
        query: {
          id: row.id
        }
      })
    }
  }
}
</script>

<style lang="scss" scoped>
.card-container {
  padding: 20px;
  background-color: #fff;
}

.search-container {
  display: flex;
  align-items: center;
  border-bottom: 1px solid rgb(237, 237, 237, .9);
  padding-bottom: 20px;
  .search-main {
    width: 220px;
    margin-right: 10px;
  }
  .search-btn{
    margin-left:20px;
  }
}
.create-container{
  margin: 10px 0px;
}
.page-container{
  padding:4px 0px;
  text-align: right;
}
.form-container{
  padding:0px 80px;
}
</style>
